<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-07-12
  Time: 12:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户地址</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/fontawesome/css/all.css" media="all">
    <style>
        .layui-table-cell {
            text-align: center;
            white-space: normal;
        }
    </style>
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">
        <form class="layui-form layui-row layui-col-space16" style="margin-bottom: 10px">
            <div class="layui-inline">
                <label class="layui-form-label">用户编号</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="userId" placeholder="请输入用户编号(数字)" class="layui-input" lay-verify="number">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">收货人姓名</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="name" placeholder="请输入姓名" class="layui-input" >
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">电话号码</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" name="phone" placeholder="请输入电话" class="layui-input" lay-verify="number">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">省份</label>
                <div class="layui-input-inline" style="width: 150px">
                    <select name="province" lay-filter="province" lay-search="" class = "province">
                        <option value="">请选择省份名称</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button id="search" class="layui-btn layui-btn-normal layui-row-xs" lay-submit lay-filter="search">查找
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-row-xs">清空</button>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
<%--                <a class="layui-btn layui-btn-danger layui-btn-sm data-count-edit" lay-event="view">批量下架</a>--%>
            </div>
        </script>

        <table class="layui-hide" id="addressTable" lay-filter="addressTable"></table>

        <script type="text/html" id="addressMap">
            <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="mapView" style="margin: auto"><i class="fa fa-location-arrow"></i> 地址显示</a>
        </script>

        <script type="text/html" id="addressTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'element','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;

        // 请求加载下拉框数据
        $.ajax({
            url: '${pageContext.request.contextPath}/manager/province/list_id_name',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                // 动态添加新的选项
                $.each(data, function(index, item) {
                    var option = $('<option></option>').val(item.id).text(item.name);
                    $('.province').append(option);
                });
                form.render();
            },
            error: function(xhr, textStatus, errorThrown) {
                alert('获取数据失败');
            }
        });

        /**
         * 表格渲染
         */
        table.render({
            elem: '#addressTable',
            autoSort: true,
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize' // 每页数据条数的参数名，默认：limit
            },
            url: '${pageContext.request.contextPath}/manager/address/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 100,fixed: 'left'},
                {title: '序号', type: "numbers", width: 80, align: "center"},
                {field: 'id', width: 120, title: '地址编号',hide:true},
                {field: 'receiver', width: 130, title: '收货人姓名', sort: true},
                {field: 'provinceName', width: 110, title: '所属省', sort: true},
                {field: 'cityName', width: 110, title: '所属市', sort: true},
                {field: 'countryName', width: 140, title: '所属区/县', sort: true},
                {field: 'addr', width: 200, title: '具体地址', sort: true},
                { width: 130, title: '地图查看',toolbar: "#addressMap"},
                {field: 'postcode', width: 110, title: '邮编'},
                {field: 'phone', width: 120, title: '电话'},
                {field: 'userId', width: 120, title: '用户编号'},
                {field: 'updateTime', width: 200, title: '更新时间'},
                {fixed: 'right', title:'操作', width: 200, toolbar: '#addressTableBar'}
            ]],
            limits: [8, 15, 20, 25, 50, 100],
            limit: 8,
            page: true,
            loading: true,
            skin: 'grid',
        });

        /**
         * 自定义验证规则
         */
        form.verify({
            number: function(value){
                if(value && isNaN(value)) return '只能填写数字';
            }
        });

        /**
         * 条件搜索表格数据重载
         */
        form.on('submit(search)', function (data) {
            //执行重载
            table.reload('addressTable', {
                page: {
                    curr: 1 //重新从第 2 页开始
                }
                , where: {
                    userId:data.field.userId,
                    receiver:data.field.name,
                    provinceId: data.field.province,
                    phone:data.field.phone
                }
            }, 'data');
            return false;
        });

        table.on('tool(addressTable)', function (obj) {
            var data = obj.data; // 获得当前行数据
            // 获取当前选中行的索引值

            // console.log(obj)
            if (obj.event === 'mapView') {
                var index = layer.open({
                    title: '',
                    type: 2,
                    shade: 0.2,
                    title: false, // 不显示标题栏
                    shadeClose: true,
                    offset: '20px',
                    area:['80%', '80%'],
                    content: '${pageContext.request.contextPath}/manager/address/map?address=' + data.provinceName + data.cityName + data.countryName + data.addr,
                });
            }
        });

        table.on('toolbar(addressTable)', function (obj) {
            switch (obj.event) {
                case 'LAYTABLE_TIPS':
                    layer.alert('输入相关信息以查询用户地址信息');
                    break;
            }
            ;
        });

    });

</script>
</body>
</html>
